package IO;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

//流对象
public class Demo2 {

    public static String PATH = "d:/new/test1.txt";

    public static void main(String[] args) throws IOException {
        //Reader

//       Reader reader = new FileReader(PATH);
//       while(true){
//           int x = reader.read();
//           if(x == -1) { break;}
//           char ch = (char) x;
//           System.out.println(x + " ");
//           System.out.print(ch + " ");
//       }
//       reader.close();

//        Reader reader = new FileReader(PATH);
//        while(true){
//            char[] cbuf = new char[1024];
//            int n = reader.read(cbuf);
//            if(n == -1) { break;}
//            System.out.println("n = " + n);
//            for(int i = 0; i < n; i++){
//                System.out.print(cbuf[i] + " ");
//            }
//        }
//        reader.close();


        //Write

//        try(Writer writer = new FileWriter(PATH)){
//            writer.write("文件IO");  //直接操作会覆盖
//        }
//
//        try(Writer writer = new FileWriter(PATH, true)){
//            writer.write("文件IO");  //添加在原文件的末尾
//        }




        //InputStream

//        try(InputStream inputStream = new FileInputStream(PATH)){
//            byte[] buffer = new byte[1024];
//            int n = inputStream.read(buffer);
//            System.out.println("n = " + n);
//            for(int i = 0; i < n; i++){
//                System.out.print((char) buffer[i] + " ");
////                System.out.printf("%x\n", buffer[i]);  // 16进制
//            }
//        }

//        try(InputStream inputStream = new FileInputStream(PATH)){
//            Scanner scanner = new Scanner(inputStream);
//            while(scanner.hasNext()){
//                String str = scanner.next();
//                System.out.print(str + " ");
//            }
//        }

//        try(InputStream inputStream = new FileInputStream(PATH)){
//            try(Scanner scanner = new Scanner(inputStream, "UTF-8")){
//                while(scanner.hasNext()){
//                    String str = scanner.next();
//                    System.out.print(str + " ");
//                }
//            }
//        }


        //OutputStream

//        try(OutputStream outputStream = new FileOutputStream(PATH)){
//            PrintWriter writer = new PrintWriter(outputStream);
//            writer.printf("hello");
//            //刷新缓冲区
//            writer.flush();
//        }

//        try(OutputStream outputStream = new FileOutputStream(PATH, true)){
//            PrintWriter writer = new PrintWriter(outputStream);
//            writer.printf("hello");
//            //刷新缓冲区
//            writer.flush();
//        }

//        //Unicode  字符
//        //UTF-8  字符串
//        try(OutputStream outputStream = new FileOutputStream(PATH, true)){
//            String str = "你好世界";
//            outputStream.write(str.getBytes(StandardCharsets.UTF_8));
//        }
    }
}
